import urllib, urllib2
import django.utils.simplejson as jsondata
import urlparse
import sys
class TweetData:
    def __init__(self): 
        self.username = 'Your Twitter User Name'
        self.password = 'Your Twitter User Password'
        self.hashTag = '[{"tag":"#your"},{"tag":"#hash"},{"tag":"#tags"}]'
    def processUrl(self,url):        
        try:
            auth_handler = urllib2.HTTPBasicAuthHandler()
            (scheme, netloc, path, params, query, fragment) = urlparse.urlparse(url)
            auth_handler.add_password('Twitter API', netloc, self.username, self.password)
            opener = urllib2.build_opener(auth_handler)
            # ...and install it globally so it can be used with urlopen.
            urllib2.install_opener(opener)
           
            f = urllib2.urlopen(url)
            json = f.read()
            json1 = jsondata.JSONDecoder().decode(json)
            return json1
        except:
            #return '[{"text":"Looks like Twitter is down. Thanks for visiting us. Please try again latter."}]'
            jdata = jsondata.JSONDecoder().decode('{"error":"Looks like Twitter is down. Thanks for visiting us. Please try again latter."}')
            return jdata
            #return sys.exc_info()[1]
            raise
        
    def processUrlWithoutAuth(self,url):
        try:
            f = urllib2.urlopen(url)
            json = f.read()
            jdata = jsondata.JSONDecoder().decode(json)
            return jdata
        except:
            jdata = jsondata.JSONDecoder().decode('{"error":"Looks like Twitter is down. Thanks for visiting us. Please try again latter."}')
            return jdata
            #return sys.exc_info()[1]
            raise  
        
    def getHashTag(self):
        hTags = jsondata.JSONDecoder().decode(self.hashTag)
        return hTags;
      
    def getDirectMessage(self):
        url = 'http://twitter.com/direct_messages.json'
        directMessage = self.processUrl(url)
        return directMessage
    
    def getTwitterSearch(self,keyword):
        url = 'http://search.twitter.com/search.json?q=%40'+keyword
    
        #f = urllib2.urlopen(url)
        #json = f.read()
        searchData = self.processUrlWithoutAuth(url)#jsondata.JSONDecoder().decode(json)
        return searchData   
    
    def getFavorites(self):
        url = 'http://twitter.com/favorites.json?count=1'
        favorites = self.processUrl(url)
        return favorites
    
    def getUserTimeLine(self):
        url = 'http://twitter.com/statuses/user_timeline/'+self.username+'.json?count=10'
        #f = urllib2.urlopen(url)
        #json = f.read()
        userTimeLine = self.processUrlWithoutAuth(url)#jsondata.JSONDecoder().decode(json)         
        #userTimeLine = self.processUrl(url)
        return userTimeLine
    
    def getFriendsDetail(self):
        url = 'http://twitter.com/statuses/friends/'+self.username+'.json?count=49'
        friendData = self.processUrlWithoutAuth(url)
        return friendData